<?php

namespace app\common\services;

use app\models\ShortMessage;
use yii\base\Exception;

class ShortMessageServices
{
    public $API_NAME = 'toujiao';
    public $API_PWD = '9FA6EB4D075A98F84F896CF0D451'; //
    public $API_URL = 'http://web.1xinxi.cn/asmx/smsservice.aspx';
    public $sign_name = '投教大观';
    public $code_status = false;
    public $content = '';
    public $msgtype = '';
    public $msg_code = '';
    public $msg_no = '';
    public $message = '';
    protected $messageCallBack = [
        0 => '短信提交成功',
        1 => '含有敏感词汇',
        2 => '余额不足',
        3 => '没有号码',
        4 => '包含sql语句',
        10 => '账号不存在',
        11 => '账号注销',
        12 => '账号停用',
        13 => 'IP鉴权失败',
        14 => '格式错误',
        -1 => '系统异常',
    ];

    public function templates($data)
    {
        foreach ($data as $key => $value) {
            $this->content = str_replace('{' . $key . '}', $value, $this->content);
        }
    }

    public function CreateCode($code, $no, $templates = '', $msgtype = '', $mobile)
    {
        $this->msg_code = $code;
        $this->msg_no = $no;
        $this->content = $templates;
        $this->templates(['code' => $this->msg_code, 'number' => $this->msg_no]);
        $this->msgtype = $msgtype;
        if ($this->sendmessage($mobile)) {
            $this->code_status = true;
        }
        $this->CodeLog($this->code_status, $this->message, $this->msg_code, $mobile);
    }

    public function CodeLog($status, $info, $code, $mobile)
    {
        try {
            $time = time();
            $status = ($status) ? 1 : 0;
            $short_msg = new ShortMessage();
            $short_msg->mobile = $mobile;
            $short_msg->codes = $code;
            $short_msg->status = $status;
            $short_msg->content = $info;
            $short_msg->send_time = $time;
            $short_msg->create_time = $time;
            $short_msg->save(0);
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    public function sendMessage($mobile)
    {
        $argv = [
            'name' => $this->API_NAME, //必填参数。用户账号
            'pwd' => $this->API_PWD, //必填参数。（web平台：基本资料中的接口密码）
            'content' => $this->content, //必填参数。发送内容（1-500 个汉字）UTF-8编码
            'mobile' => $mobile, //必填参数。手机号码。多个以英文逗号隔开
            'stime' => '', //可选参数。发送时间，填写时已填写的时间发送，不填时为当前时间发送
            'sign' => $this->sign_name, //必填参数。用户签名。
            'type' => 'pt', //必填参数。固定值 pt
            'extno' => '1', //可选参数，扩展码，用户定义扩展码，只能为数字
        ];

        $result = HttpClient::post($this->API_URL, $argv);
        $num = substr($result, 0, 1);
        if (isset($this->messageCallBack[$num])) {
            $this->message = '发送失败 （接口失败）:' . $this->messageCallBack[$num];
        }

        if ($num == '0') {
            $this->message = $result;
            return true;
        } else {
            return false;
        }
    }

    public function CheckCode($code)
    {
        $short_msg = ShortMessage::find()->where(['codes' => $code])->one();
        if (!$short_msg) {
            return ['code' => -1, 'msg' => '验证码错误'];
        } else {
            if (time() - $short_msg->send_time > 300) {
                return ['code' => -2, 'msg' => '验证码失效,请重新获取'];
            }
            if ($short_msg->is_use == 1) {
                return ['code' => -3, 'msg' => '验证码已经使用过'];
            }
            return ['code' => 200, 'msg' => '验证成功'];
        }
    }

    public function UseCode($code)
    {
        $short_msg = ShortMessage::find()->where(['codes' => $code])->one();
        $short_msg->is_use = 1;
        $short_msg->save(0);
    }
}